package sqlitedb

import (
	"fmt"
	"strconv"

	us "gitee.com/gouyabin/go_utils"
)

const (
	GroupDataDB         = "GroupList"
	UserGroupRelevantDB = "UserGroupRelevant"
	AddrGroupRelevantDB = "AddrGroupRelevant"
)

type Group struct {
	Id       int    `sqlField:"id" sqlType:"integer PRIMARY KEY autoincrement"` //ID主键
	GName    string `sqlField:"gname" sqlType:"CHAR(32)" json:"gname"`
	Role     int    `sqlField:"role" sqlType:"integer" json:"role"`
	Gremarks string `sqlField:"gremarks" sqlType:"CHAR(32)" json:"gremarks"`
}

//根据用户ID查找关联组
func (*Group) FindUserIDGroup(userid int) []Group {
	db := CONN
	// SELECT t.T_name,t.COURSE,s.S_name,s.SCORE FROM TEACHER AS t INNER JOIN STUDENT AS s ON t.C_id = s.C_id WHERE s.SCORE > 60;
	sqlCmd := "SELECT g.id,g.gname,g.Role,g.Gremarks FROM " + UserGroupRelevantDB + " as ug INNER JOIN " + GroupDataDB + " AS g ON ug.groupid = g.id where ug.userid=" + strconv.Itoa(userid)
	fmt.Println("开始查找Auth", sqlCmd)
	rows, err := db.Query(sqlCmd)
	if err != nil {
		us.WriteLogV2("SqliteDB 用户组查询错误：" + strconv.Itoa(userid))
		return []Group{}
	}
	defer rows.Close()
	GList := []Group{}
	for rows.Next() {
		p := new(Group)
		err := rows.Scan(&p.Id, &p.GName, &p.Role, &p.Gremarks)
		us.CheckErr(err)
		GList = append(GList, *p)
		// fmt.Println(p.Id, p.DirName, p.ParentId)
	}
	fmt.Println("查询到的组列表:", GList)
	return GList

}

//根据用户id查询是否有配置权限
func (g *Group) UserIDFindConfig(userid int) bool {
	//根据UserID查询关联组
	gList := g.FindUserIDGroup(userid)
	for _, value := range gList {
		if value.Role == 0 {
			fmt.Println("有配置权限.")
			return true
		}
	}
	fmt.Println("无配置权限.")
	return false
}

//添加组
func (g *Group) Add() int64 {
	fmt.Println("添加组")
	sqlCmd := "INSERT INTO " + GroupDataDB + "(gname, role, gremarks) values(?,?,?)"
	fmt.Println("sqlCmd：", sqlCmd)
	stmt, err := CONN.Prepare(sqlCmd)
	us.CheckErr(err)
	res, err := stmt.Exec(g.GName, g.Role, g.Gremarks)
	us.CheckErr(err)
	id, err := res.LastInsertId()
	us.CheckErr(err)
	fmt.Println(id)
	return id
}

type UserGroupRelevant struct {
	Id      int `sqlField:"id" sqlType:"integer PRIMARY KEY autoincrement"` //ID主键
	UserID  int `sqlField:"userid" sqlType:"integer" json:"userid"`
	GroupID int `sqlField:"groupid" sqlType:"integer" json:"groupid"`
}

//添加用户组权限
func (g *UserGroupRelevant) Add() int64 {
	fmt.Println("添加用户组权限")
	sqlCmd := "INSERT INTO " + UserGroupRelevantDB + "(userid, groupid) values(?,?)"
	stmt, err := CONN.Prepare(sqlCmd)
	us.CheckErr(err)
	res, err := stmt.Exec(g.UserID, g.GroupID)
	us.CheckErr(err)
	id, err := res.LastInsertId()
	us.CheckErr(err)
	fmt.Println(id)
	return id
}

//地点与组相关
type AddrGroupRelevant struct {
	Id      int `sqlField:"id" sqlType:"integer PRIMARY KEY autoincrement"` //ID主键
	DId     int `sqlField:"did" sqlType:"integer" json:"did"`
	GroupID int `sqlField:"groupid" sqlType:"integer" json:"groupid"`
}

//创建组列表
func createGroupTypeDB() {
	tableName := GroupDataDB
	GroupName := Group{
		Id:       1,
		GName:    "Admin",
		Role:     0,
		Gremarks: "管理员",
	}
	fmt.Println("开始创建用户组表", tableName)
	createTablesStruct(GroupName, tableName)
	GroupName.Add()
}

//创建用户权限组列表
func createUserGroupTypeDB() {
	tableName := UserGroupRelevantDB
	UserGroup := UserGroupRelevant{
		Id:      1,
		UserID:  1,
		GroupID: 1,
	}
	fmt.Println("创建用户权限组列表", tableName)
	createTablesStruct(UserGroup, tableName)
	UserGroup.Add()
}

//创建地点权限组列表
func createAddrGroupTypeDB() {
	tableName := AddrGroupRelevantDB
	AddrGroup := AddrGroupRelevant{
		Id:      1,
		DId:     1,
		GroupID: 1,
	}
	fmt.Println("创建地点权限组列表", tableName)
	createTablesStruct(AddrGroup, tableName)
}
